package com.fw.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.annotation.Log;
import com.fw.core.controller.BaseController;
import com.fw.core.domain.AjaxResult;
import com.fw.core.page.TableDataInfo;
import com.fw.enums.BusinessType;
import com.fw.mes.Result;
import com.fw.system.admin.domain.TbAddress;
import com.fw.system.admin.domain.TbCityCode;
import com.fw.system.admin.domain.TbConutrySort;
import com.fw.system.admin.domain.TbUser;
import com.fw.system.admin.service.ITbAddressService;
import com.fw.system.admin.service.ITbCityCodeService;
import com.fw.system.admin.service.ITbConutrySortService;
import com.fw.system.admin.service.ITbUserService;
import com.fw.utils.poi.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

import static com.fw.mes.ResultUtils.success;

/**
 * 客户端用户Controller
 *
 * @author yanwei
 * @date 2023-04-04
 */
@Api(tags = "user")
@RestController
@RequestMapping("/admin/user")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class TbUserController extends BaseController {
    @Autowired
    private ITbUserService tbUserService;

    @Autowired
    private ITbCityCodeService cityCodeService;
    @Autowired
    private ITbConutrySortService conutrySortService;

    @Autowired
    private ITbAddressService addressService;

    /**
     * 查询客户端用户列表
     */
    @PreAuthorize("@ss.hasPermi('admin:user:list')")
    @GetMapping("/list")
    @ApiOperation("查询客户端用户列表")
    public TableDataInfo list(TbUser tbUser) {
        startPage();
        List<TbUser> list = tbUserService.selectTbUserList(tbUser);
        for (TbUser user : list) {
            Long nowCityCount = user.getNowCityCount();
            if (Objects.isNull(nowCityCount))
                continue;
            user.setCityCodeList(cityCodeService.list(Wrappers.<TbCityCode>lambdaQuery().le(TbCityCode::getSortCode, nowCityCount).orderByDesc(TbCityCode::getSortCode)));
        }
        return getDataTable(list);
    }

    /**
     * 导出客户端用户列表
     */
    @PreAuthorize("@ss.hasPermi('admin:user:export')")
    @Log(title = "客户端用户", businessType = BusinessType.EXPORT)
    @ApiOperation("导出客户端用户列表")
    @GetMapping("/export")
    public AjaxResult export(TbUser tbUser) {
        List<TbUser> list = tbUserService.selectTbUserList(tbUser);
        ExcelUtil<TbUser> util = new ExcelUtil<TbUser>(TbUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取客户端用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:user:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取客户端用户详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(tbUserService.selectTbUserById(id));
    }

    /**
     * 新增客户端用户
     */
    @PreAuthorize("@ss.hasPermi('admin:user:add')")
    @Log(title = "客户端用户", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增客户端用户")
    public AjaxResult add(@RequestBody TbUser tbUser) {
        return toAjax(tbUserService.insertTbUser(tbUser));
    }

    /**
     * 修改客户端用户
     */
    @PreAuthorize("@ss.hasPermi('admin:user:edit')")
    @Log(title = "客户端用户", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改客户端用户")
    public AjaxResult edit(@RequestBody TbUser tbUser) {
        return toAjax(tbUserService.updateTbUser(tbUser));
    }

    /**
     * 删除客户端用户
     */
    @PreAuthorize("@ss.hasPermi('admin:user:remove')")
    @Log(title = "客户端用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除客户端用户")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tbUserService.deleteTbUserByIds(ids));
    }


    //全国排行
    @GetMapping("/countrySort")
    @ApiOperation("查询全国排行")
    public Result countrySort(@RequestParam(defaultValue = "1", required = false) Integer pageNum, @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbConutrySort> list = conutrySortService.list(Wrappers.<TbConutrySort>lambdaQuery().orderByDesc(TbConutrySort::getCountFool).orderByDesc(TbConutrySort::getNowCountCity).orderByAsc(TbConutrySort::getCountTime));
        PageInfo<TbConutrySort> pageInfo = new PageInfo<>(list);
        List<TbConutrySort> pageInfoList = pageInfo.getList();
        for (TbConutrySort tbConutrySort : pageInfoList) {
            TbAddress tbAddress = addressService.getOne(Wrappers.<TbAddress>lambdaQuery().eq(TbAddress::getUserId, tbConutrySort.getUserId()));
            if (Objects.nonNull(tbAddress)){
                tbConutrySort.setAddrDetail(tbAddress.getAddrDetail());
                tbConutrySort.setAddrName(tbAddress.getAddrName());
                tbConutrySort.setPhone(tbAddress.getPhone());
                tbConutrySort.setProName(tbAddress.getProName());
            }

        }

        return success(pageInfo);
    }

    /**
     * 导出查询全国排行
     */
    @PreAuthorize("@ss.hasPermi('admin:user:export')")
    @Log(title = "查询全国排行", businessType = BusinessType.EXPORT)
    @ApiOperation("导出查询全国排行")
    @GetMapping("/exportCountrySort")
    public AjaxResult exportCountrySort() {
        List<TbConutrySort> list = conutrySortService.list(Wrappers.<TbConutrySort>lambdaQuery().orderByDesc(TbConutrySort::getCountFool).orderByDesc(TbConutrySort::getNowCountCity).orderByAsc(TbConutrySort::getCountTime));
        for (TbConutrySort tbConutrySort : list) {
            TbAddress tbAddress = addressService.getOne(Wrappers.<TbAddress>lambdaQuery().eq(TbAddress::getUserId, tbConutrySort.getUserId()));
            if (Objects.nonNull(tbAddress)){
                tbConutrySort.setAddrDetail(tbAddress.getAddrDetail());
                tbConutrySort.setAddrName(tbAddress.getAddrName());
                tbConutrySort.setPhone(tbAddress.getPhone());
                tbConutrySort.setProName(tbAddress.getProName());
            }

        }
        ExcelUtil<TbConutrySort> util = new ExcelUtil<TbConutrySort>(TbConutrySort.class);
        return util.exportExcel(list, "全国排行");
    }
}
